package com.jk.myimkit.imkit.plugin;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import com.jk.myimkit.R;
import com.jk.myimkit.imkit.RongExtension;
import com.jk.myimkit.imkit.plugin.location.AMapRealTimeActivity;
import com.jk.myimkit.imkit.plugin.location.LocationManager;

public class RealTimeLocationPlugin implements IPluginModule {
    public RealTimeLocationPlugin() {
    }

    public Drawable obtainDrawable(Context context) {
        return ContextCompat.getDrawable(context, R.drawable.rc_ext_plugin_location_selector);
    }

    public String obtainTitle(Context context) {
        return context.getString(R.string.rc_plugin_location_sharing);
    }

    public void onClick(Fragment currentFragment, RongExtension extension) {
        int perCoarse = currentFragment.getActivity().checkCallingOrSelfPermission("android.permission.ACCESS_COARSE_LOCATION");
        int perFine = currentFragment.getActivity().checkCallingOrSelfPermission("android.permission.ACCESS_FINE_LOCATION");
        int perNetwork = currentFragment.getActivity().checkCallingOrSelfPermission("android.permission.ACCESS_NETWORK_STATE");
        if (perCoarse != -1 && perFine != -1 && perNetwork != -1) {
            int result = LocationManager.getInstance().joinLocationSharing();
            if (result == 0) {
                Intent intent = new Intent(currentFragment.getActivity(), AMapRealTimeActivity.class);
                currentFragment.getActivity().startActivity(intent);
            } else if (result == 1) {
                Toast.makeText(currentFragment.getActivity(), R.string.rc_network_exception, Toast.LENGTH_SHORT).show();
            } else if (result == 2) {
                Toast.makeText(currentFragment.getActivity(), R.string.rc_location_sharing_exceed_max, Toast.LENGTH_SHORT).show();
            }

        } else {
            Toast.makeText(currentFragment.getActivity(), currentFragment.getActivity().getResources().getString(R.string.rc_ext_location_permission_failed), Toast.LENGTH_SHORT).show();
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    }
}

